//
//  UITableViewCell+HDArea.m
//  app
//
//  Created by D on 15/5/13.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDAreaCell.h"
#import "HDCityButton.h"
#import "HDAreaModel.h"

@implementation HDAreaCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.subArray = [NSArray array];
    }
    return self;
}

-(void)cellWithArray:(NSArray *)array
{
    CGFloat threeEachWith = SCREEN_WIDTH/3;
    CGFloat threeEachHight = threeEachWith/2;
    self.subArray = array;
    
    for (int j = 0 ; j < array.count ; j ++) {
        
        HDCityButton * cityBtn = [[HDCityButton alloc]initWithFrame:CGRectMake(j%3*threeEachWith, j/3*threeEachHight, threeEachWith+UP(1), threeEachHight+UP(1))];
        HDAreaModel * cityModel = array[j];
//        NSLog(@"name = %@",array[j]);
        [cityBtn setTitle:[NSString stringWithFormat:@"%@",cityModel.areaName] forState:UIControlStateNormal];
        cityBtn.tag = cityModel.aid.integerValue;
        cityBtn.titleLabel.font = JKAppGlobleFontMenu;
        [cityBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cityBtn addTarget:self action:@selector(citySub:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self addSubview:cityBtn];
        
        
    }
    _cellHight = [self heightOfCellWithSubBtnCount:array.count];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, _cellHight);
    
    [self.delegate cityCellHigth:_cellHight];
}
-(void)drawRect:(CGRect)rect
{
    CGFloat threeEachWith = SCREEN_WIDTH/3;
    CGFloat threeEachHight = threeEachWith/2;
    
    if (self.subArray.count%3 == 0) {
        for (int i = 0 ; i < self.subArray.count/3-1; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
            
        }
        for (int j = 0 ; j < 2; j++) {
            [self drawSingleLineFromPoint:CGPointMake(threeEachWith*(j+1), 0) toPoint:CGPointMake(threeEachWith*(j+1), _cellHight) color:nil];
            
        }
    }else if (self.subArray.count%3 == 1)
    {
        for (int i = 0 ; i < self.subArray.count/3; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
        }
        [self drawSingleLineFromPoint:CGPointMake(threeEachWith, 0) toPoint:CGPointMake(threeEachWith, _cellHight) color:nil];
        [self drawSingleLineFromPoint:CGPointMake(threeEachWith*2, 0) toPoint:CGPointMake(threeEachWith*2, _cellHight-threeEachHight) color:nil];
    }else if (self.subArray.count%3 == 2)
    {
        for (int i = 0 ; i < self.subArray.count/3; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
        }
        for (int j = 0 ; j < 2; j++) {
            [self drawSingleLineFromPoint:CGPointMake(threeEachWith*(j+1), 0) toPoint:CGPointMake(threeEachWith*(j+1), _cellHight) color:nil];
            
        }
    }
    
}
-(CGFloat)heightOfCellWithSubBtnCount:(NSInteger)count
{
    if (count%3 == 0) {
        return SCREEN_WIDTH/3/2*(count/3);
    }else
        return SCREEN_WIDTH/3/2*(count/3+1);
}

-(void)citySub:(HDCityButton*)sender
{
    NSLog(@"1111%@",sender.titleLabel.text);
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectCellButton:)]) {
        
        [self.delegate selectCellButton:sender];
        
    }
}

@end
